<?php
/**
 * 文章
 */
class ProductModule
{
	public function index()
	{
        global $_FANWE;
        $cate_id = intval($_GET['cate']);
        $sql = 'SELECT * FROM '.FDB::table('goods_category').' WHERE cate_id = '.$cate_id.' AND status <>0';
        $r = FDB::fetchFirst($sql);
        if(!$r){
            fHeader('location: '.FU('index'));
            exit;
        }

        $parent_id = $r['parent_id'] == 0 ? $cate_id : $r['parent_id'];
        $sql = 'SELECT * FROM '.FDB::table('goods_category').' WHERE parent_id='.$parent_id . ' AND status <>0 ORDER BY sort ASC';
        $cates = FDB::fetchAll($sql);
        $catesmap = array();
        $cate_ids = array($cate_id);
        foreach($cates as $v) {
            $cate_ids[] = $v['cate_id'];
            $catesmap[$v['cate_id']] = $v;
        }
        $where = 'flag=0 AND cate_id IN('.($r['parent_id'] == 0 ? implode(',', $cate_ids) : $cate_id).')';
        if(isset($_GET['minprice']) && $_GET['minprice'] !== '') {
            $minprice = intval($_GET['minprice']);
            $where .= ' AND price>='.$minprice;
        }
        if(isset($_GET['maxprice']) && $_GET['maxprice'] !== '') {
            $maxprice = intval($_GET['maxprice']);
            $where .= ' AND price<='.$maxprice;
        }
        $keywordsshow = array();
        $keywords = empty($r['search_keywords']) ? array() : explode(' ', $r['search_keywords']);
        $ids = empty($_GET['ids']) && $_GET['ids'] !== '0' ? array() : explode(',', $_GET['ids']);
        foreach($ids as $k => $id) {
            if(!is_numeric($id))
                unset($ids[$k]);
        }
        if(!empty($ids)) {
            $where .= ' AND (1';
            foreach($ids as $id) {
                if(isset($keywords[$id])) {
                    $where .= " AND title LIKE '%{$keywords[$id]}%'";
                    $keywordsshow[$id] = array('id' => $id, 'keyword' => $keywords[$id], 'checked' => true);
                }
            }
            $where .= ')';
        }
        foreach($keywords as $key => $v) {
            if(!isset($keywordsshow[$key])) {
                $keywordsshow[$key] = array('id' => $key, 'keyword' => $v, 'checked' => false);
            }
        }
        
		$orderkeys = array('sharenum', 'commentnum', 'priceup', 'pricedown');
		$orders = array('sharenum' => 'share_num DESC', 'commentnum' => 'comment_count DESC', 'default' => 'orderby DESC,id DESC', 'priceup' => 'price ASC', 'pricedown' => 'price DESC');
		$orderkey = in_array($_GET['order'], $orderkeys) ? $_GET['order'] : 'default';
		$order = $orders[$orderkey];
		
        $sql = 'SELECT COUNT(id) FROM '.FDB::table('wogu_product').' WHERE '.$where;
        $count = FDB::resultFirst($sql);
        $page_args = array();
        $pager = buildPage('product/'.ACTION_NAME,$page_args,$count,$_FANWE['page'],15);
        $product_list = array();
        if($_FANWE['page'] <= $pager['page']) {
            $pb_page_args = $page_args;
            $pb_page_args['page'] = $_FANWE['page'];
            $pb_page_args['pindex'] = '_pindex_';
            $pb_page_args['cate'] = $cate_id;
            if(isset($_GET['minprice']) && $_GET['minprice'] !== '') {
                $pb_page_args['minprice'] = $minprice;
            }
            if(isset($_GET['maxprice']) && $_GET['maxprice'] !== '') {
                $pb_page_args['maxprice'] = $maxprice;
            }
            if(!empty($_GET['ids'])) {
                $pb_page_args['ids'] = implode(',', $ids);
            }
            $pb_page_args['order'] = $orderkey;
            $pb_url = $_FANWE['site_root'].'services/service.php?m=product&a=list&'.http_build_query($pb_page_args);
            $sql = 'SELECT * FROM '.FDB::table('wogu_product').' WHERE '.$where.' ORDER BY '.$order.' LIMIT '.$pager['limit'];
            require FANWE_ROOT.'core/function/wogu.func.php';
            $product_list = FDB::fetchAll($sql);
            foreach($product_list as $k => &$v) {
                $v['price'] = sprintf('%.2f', $v['price']);
                if(isset($_GET['ajax'])) {
                    $v['img'] = create_mob_img($v['img']);
                }
            }
            //tags
            $sql = 'SELECT tag_id FROM '.FDB::table('goods_category_tags').' WHERE cate_id='.$parent_id . ' ORDER BY sort ASC';
            $gctags = FDB::fetchAll($sql);
            $tagids = $tag_list = array();
            foreach($gctags as $v) {
                $tagids[] = $v['tag_id'];
            }
            if(!empty($tagids)) {
                $sql = 'SELECT * FROM '.FDB::table('goods_tags').' WHERE tag_id IN ('.implode(',', $tagids).')';
                $tag_list = FDB::fetchAll($sql);
            }

            // foreach ($product_list as $key => $value) {
            //     $product_list[$key]['author'] = $name_list[$value['uid']]['user_name'];
            //     $product_list[$key]['create_time_show'] = date('y/m/d', $value['create_time']);
            // }
        }

        if(isset($_GET['ajax'])) {
            echo json_encode(array(
                'list' => $product_list,
                'count' => $count
            ));
        } else {
            include template('page/product/product_index');
            display();
            exit;
        }
	}
	
    public function show()
    {
        global $_FANWE;
        $id = $_FANWE['request']['id'];
        if(!is_numeric($id)){
            fHeader('location: '.FU('index'));
            exit;
        }
        $share_detail = FS('Product')->getShareDetail($id);
        if($share_detail['jb_num'] >= 1) {
            fHeader('location: /missing.html');
            exit;
        }
        $share_detail['price'] = sprintf('%.2f', $share_detail['price']);
        $_FANWE['nav_title'] = $share_detail['title'];
        $_FANWE['seo_keywords'] = $share_detail['title'];
        $_FANWE['seo_description'] = $share_detail['title'];

        //评论
		$share_detail['comments'] = FS('Product')->getShareCommentList($id,'0,10');
		//分享评论分页
		$pager = buildPage('',array(),$share_detail['comment_count'],$_FANWE['page'],10);
		//评论数最多的
        $sql = 'SELECT * FROM '.FDB::table('wogu_product').' ORDER BY comment_count DESC LIMIT 3';
        $products_comment = FDB::fetchAll($sql);
		//转发最多的
        $sql = 'SELECT * FROM '.FDB::table('wogu_product').' ORDER BY share_num DESC LIMIT 3';
        $products_share = FDB::fetchAll($sql);
        
        $currcate = $_FANWE['cache']['goods_category']['all'][$share_detail['cate_id']];
        $parentcate = $_FANWE['cache']['goods_category']['all'][$currcate['parent_id']];
		
        $_FANWE['PAGE_SEO_SELF'] = array(
			'title'=>$share_detail['title'],
			'seo_title'=>$share_detail['seo_title'],
			'seo_keywords'=>$share_detail['seo_keywords'],
			'seo_description'=>$share_detail['seo_description'],
	    );

        if(isset($_GET['ajax'])) {
            echo json_encode($share_detail);
        } else {
            include template('page/product/product_show');
            display();
            exit;
        }
    }
    
    public function cart() {
        global $_FANWE;
        $pids = empty($_COOKIE['pids']) ? array() : explode(',', $_COOKIE['pids']);
        foreach($pids as $k => $v) {
            if(!is_numeric($v))
                unset($pids[$k]);
        }
        $format_products = $cateinfo = array();
        if(!empty($pids)) {
            $sql = 'SELECT * FROM '.FDB::table('wogu_product').' WHERE id IN('.implode(',', $pids).')';
            $products = FDB::fetchAll($sql);
            $cateids = array();
            foreach($products as $v) {
                $format_products[$v['cate_id']][] = $v;
                $cateids[] = $v['cate_id'];
            }
            $cateids = array_unique($cateids);
            $sql = 'SELECT * FROM '.FDB::table('goods_category').' WHERE cate_id IN('.implode(',', $cateids).')';
            $cates = FDB::fetchAll($sql);
            $parentids = array();
            foreach($cates as $v) {
                $cateinfo[$v['cate_id']] = $v;
                if($v['parent_id'] != 0) {
                    $parentids[] = $v['parent_id'];
                }
            }
            if(!empty($parentids)) {
                $parentids = array_unique($parentids);
                $sql = 'SELECT * FROM '.FDB::table('goods_category').' WHERE cate_id IN('.implode(',', $parentids).')';
                $cates = FDB::fetchAll($sql);
                foreach($cates as $v) {
                    $cateinfo[$v['cate_id']] = $v;
                }
            }
        }
        
        include template('page/product/product_cart');
        display();
    }
    
    public function jb() {
        global $_FANWE;
        
        $id = intval($_GET['id']);
        if($id) {
            $sql = 'UPDATE '.FDB::table('product').' SET jb_num=jb_num+1 WHERE id='.$id;
            FDB::query($sql);
        }
        echo '举报成功';
    }
}
?>